function message(msgcode, param) {
	for (var i=0; i<param.length; i++) {
		eval("msgcode = msgcode.replace(/\\{"+(i+1)+"\\}/g, '"+param[i]+"');");	
	}
	return msgcode;
}

function setSelectedIndex(s, v) { 
//alert("enter setIndex");
    for ( var i = 0; i < s.options.length; i++ ) {
    	//alert("option[" + s.options[i].value + '] value[' + v + ']' );
        if ( s.options[i].value == v ) {
            s.options[i].selected = true;
            return;
        }
    }
}

if (AjaxUtil == null ) var AjaxUtil = {};
AjaxUtil.addEvent = function(el, evname, func) {
	// make sure only one function pointer will be attach to event
	AjaxUtil.removeEvent(el, evname, func);
	
	if (el.attachEvent) { // IE
		el.attachEvent("on" + evname, func);
	} else if (el.addEventListener) { // Gecko / W3C
		el.addEventListener(evname, func, true);
	} else {
		el["on" + evname] = func;
	}
};

AjaxUtil.removeEvent = function(el, evname, func) {
	if (el.detachEvent) { // IE
		el.detachEvent("on" + evname, func);
	} else if (el.removeEventListener) { // Gecko / W3C
		el.removeEventListener(evname, func, true);
	} else {
		el["on" + evname] = null;
	}
};

AjaxUtil.removeAllOptions = function(dropDown, keepTopOption) {
	var dropDownObj = document.getElementById(dropDown);
	var topOption = null;
	if ( keepTopOption && dropDownObj.length > 0 ) {
		topOption = dropDownObj.options[0];
	}
	dwr.util.removeAllOptions(dropDown);
	if ( keepTopOption && topOption ) {
		dropDownObj.options[0] = topOption;
	}
}

AjaxUtil.removeAllOptions2 = function(dropDown, keepTopOption) {
	var dropDownObj = document.getElementById(dropDown);
	var topOption = null;
	if ( keepTopOption && dropDownObj.length > 0 ) {
		topOption = new Option("-- Select --", "");
	}
	dwr.util.removeAllOptions(dropDown);
	if ( keepTopOption && topOption ) {
		dropDownObj.options[0] = topOption;
	}
}

AjaxUtil.removeOptionItem = function(dropDownObj, key) {
	for (var i=0; i<dropDownObj.length; i++) {
		if(dropDownObj.options[i].value == key) {
			dropDownObj.remove(i);
		}
	}
}

AjaxUtil.addOptionItem = function(dropDownObj, text, value) {
	var etmp=document.createElement('option');
	etmp.text = text;
	etmp.value = value;
	dropDownObj.add(etmp);
}

AjaxUtil.isValidOptionKey = function(dropDownObj, key) {
	for (var i=0; i<dropDownObj.length; i++) {
		if(dropDownObj.options[i].value == key) {
			return true;
		}
	}
	return false;
}

AjaxUtil.preHookOptions = function(dropDown, keepTopOption) {
	var dropDownObj = document.getElementById(dropDown);
	var topOption = null;
	if ( keepTopOption && dropDownObj.length > 0 ) {
		topOption = dropDownObj.options[0];
	}
	dwr.util.removeAllOptions(dropDown);
	dropDownObj.disabled = true;
	if ( keepTopOption && topOption ) {
		dropDownObj.options[0] = topOption;
		dropDownObj.options[1] = new Option("Loading ...", '');
		dropDownObj.options[1].selected = true;
	}
	else {
		dropDownObj.options[0] = new Option("Loading ...", '');
		dropDownObj.options[0].selected = true;
	}
}

AjaxUtil.setDropDown = function(dropDown, hiddenField, data, valueCol, textCol, customCallback) {
	var dropDownObj = document.getElementById(dropDown);
	var hiddenFieldObj = document.getElementById(hiddenField);
	if ( dropDownObj.length > 0 ) {
		dropDownObj[dropDownObj.length-1] = null;
	}
	dwr.util.addOptions(dropDown, data, valueCol, textCol);
	dropDownObj.disabled = false;
	if ( hiddenFieldObj.value ) dwr.util.setValue(dropDown, hiddenFieldObj.value);
	hiddenFieldObj.value = dropDownObj.value;
	AjaxUtil.addEvent(dropDownObj, 'change', function(){ 
		hiddenFieldObj.value = dropDownObj.value; 
		if(customCallback!=null)
			customCallback();
	});
};

AjaxUtil.setPairSelectDropDown = function(firstOptions, firstValue, firstAjaxFn, firstKey, firstText, secondOptions, secondValue, secondAjaxFn, secondKey, secondText) {
	var firstOptionsObj = document.getElementById(firstOptions);
	var firstValueObj = document.getElementById(firstValue);
	var secondOptionsObj = document.getElementById(secondOptions);
	var secondValueObj = document.getElementById(secondValue);

	var secondPreHook = function () {
		AjaxUtil.preHookOptions(secondOptions, true);
	};
	var secondCallback = function ( data ) {
		AjaxUtil.setDropDown(secondOptions, secondValue, data, secondKey, secondText);
	};

	var firstPreHook = function () {
		AjaxUtil.preHookOptions(firstOptions, true);
	};
	var firstCallback = function ( data ) {
		AjaxUtil.setDropDown(firstOptions, firstValue, data, firstKey, firstText);
		firstOnChange();
		AjaxUtil.addEvent(firstOptionsObj, 'change', firstOnChange);
	};
	var firstOnChange = function() {
		firstValueObj.value = dwr.util.getValue(firstOptions);
		if ( firstValueObj.value && firstValueObj.value != '') {
			secondAjaxFn(firstValueObj.value, {preHook:secondPreHook, callback:secondCallback});
		}
		else {
			AjaxUtil.removeAllOptions(secondOptions, true);
			secondValueObj.value = '';
		}		
	};
	firstAjaxFn({preHook:firstPreHook, callback:firstCallback});	
};
//--------Sample ------//
AjaxUtil.checkExistUser = function(username) {
	var d = false;
	var callbackFn = function ( data ) {
		d =  data;
	}
	dwrDataSupportService.checkExistUser(username,{callback:callbackFn,async:false});
	return d;
};
AjaxUtil.getFirstName = function(username) {
	var d ="";
	var callbackFn = function ( data ) {
		d =  data;
	}
	dwrDataSupportService.getFirstName(username,{callback:callbackFn,async:false});
	return d;
};
AjaxUtil.getLastName = function(username) {
	var d ="";
	var callbackFn = function ( data ) {
		d =  data;
	}
	dwrDataSupportService.getLastName(username,{callback:callbackFn,async:false});
	return d;
};
AjaxUtil.setRolesDropDown = function(dropDown,username, customCallback) {
	var preHookFn = function () {
		AjaxUtil.preHookOptions(dropDown, true);
	}
	var callbackFn = function ( data ) {
		AjaxUtil.setDropDown(dropDown,dropDown, data, "value", "label", null);
	}
	dwrDataSupportService.getRoles(username,{preHook:preHookFn, callback:callbackFn,async:false});
};
AjaxUtil.setFunctionsDropDown = function(dropDown,roleId, customCallback) {
	var preHookFn = function () {
		AjaxUtil.preHookOptions(dropDown, true);
	}
	var callbackFn = function ( data ) {
		AjaxUtil.setDropDown(dropDown,dropDown, data, "value", "label", null);
	}
	dwrDataSupportService.getFunctions(roleId,{preHook:preHookFn, callback:callbackFn});
};
AjaxUtil.checkExistRole = function(rolename) {
	var d = false;
	var callbackFn = function ( data ) {
		d =  data;
	}
	dwrDataSupportService.checkExistRole(rolename,{callback:callbackFn,async:false});
	return d;
};
AjaxUtil.checkUserInRole = function(rolename) {
	var d = false;
	var callbackFn = function ( data ) {
		d =  data;
	}
	dwrDataSupportService.checkUserInRole(rolename,{callback:callbackFn,async:false});
	return d;
};

//--------CDM ------//
AjaxUtil.setAllRoles = function(dropDown, hiddenField) {
	var preHookFn = function () {
		AjaxUtil.preHookOptions(dropDown, false);
	}
	var callbackFn = function ( data ) {
		AjaxUtil.setDropDown(dropDown, hiddenField, data, "value", "label");
	}
	supportService.getAllRoles({preHook:preHookFn, callback:callbackFn,async:false});
};

AjaxUtil.setAllRoles2 = function(dropDown, hiddenField) {
	var preHookFn = function () {
		AjaxUtil.preHookOptions(dropDown, false);
	}
	var callbackFn = function ( data ) {
		AjaxUtil.setDropDown(dropDown, hiddenField, data, "label", "label");
	}
	supportService.getAllRoles({preHook:preHookFn, callback:callbackFn,async:false});
};

AjaxUtil.setAllUserName = function(dropDown, hiddenField) {
	var preHookFn = function () {
		AjaxUtil.preHookOptions(dropDown, true);
	}
	var callbackFn = function ( data ) {
		AjaxUtil.setDropDown(dropDown, hiddenField, data, "value", "label");
	}
	supportService.getAllUserName({preHook:preHookFn, callback:callbackFn,async:false});
};

AjaxUtil.setAllMcaStatus = function(dropDown, hiddenField) {
	var preHookFn = function () {
		AjaxUtil.preHookOptions(dropDown, true);
	}
	var callbackFn = function ( data ) {
		AjaxUtil.setDropDown(dropDown, hiddenField, data, "value", "label");
	}
	supportService.getAllMcaStatus({preHook:preHookFn, callback:callbackFn,async:false});
};

AjaxUtil.confirmCustomer = function(system, customerNumber, recordId, groupId) {
	var result = false;
	var callbackFn = function ( data ) {
		result = data;
	};
	supportService.confirmCustomer(system, customerNumber, recordId, groupId, {callback:callbackFn,async:false});
	return result;
};

AjaxUtil.rejectReasonCustomer = function(system, customerNumber, recordId, groupId, rejectReasonId) {
	var result = false;
	var callbackFn = function ( data ) {
		result = data;
	};
	supportService.rejectReasonCustomer(system, customerNumber, recordId, groupId, rejectReasonId, {callback:callbackFn, async:false});
	return result;
};

AjaxUtil.waitInfoReasonCustomer = function(system, customerNumber, recordId, groupId, waitInfoReasonCustomer) {
	var result = false;
	var callbackFn = function ( data ) {
		result = data;
	};
	supportService.waitInfoReasonCustomer(system, customerNumber, recordId, groupId, waitInfoReasonCustomer, {callback:callbackFn, async:false});
	return result;
};

AjaxUtil.getExternalCustomerInfo = function(recordId) {
	var result;
	var callbackFn = function ( data ) {
		result = data;
	};
	supportService.getExternalCustomerInfo(recordId, {callback:callbackFn, async:false});
	return result;
};

AjaxUtil.saveExternalCustomerInfo = function(recordId, idNumber, prefixNameSurname) {
	var callbackFn = function ( data ) {
		///
	};
	supportService.saveExternalCustomerInfo(recordId, idNumber, prefixNameSurname, {callback:callbackFn, async:false});
};


AjaxUtil.setAllRejectReason = function(dropDown, hiddenField) {
	var preHookFn = function () {
		AjaxUtil.preHookOptions(dropDown, true);
	};
	var callbackFn = function ( data ) {
		AjaxUtil.setDropDown(dropDown, hiddenField, data, "value", "label");
	};
	supportService.getAllRejectReason({preHook:preHookFn, callback:callbackFn,async:false});
};
AjaxUtil.setAllWaitInfoReason = function(dropDown, hiddenField) {
	var preHookFn = function () {
		AjaxUtil.preHookOptions(dropDown, true);
	};
	var callbackFn = function ( data ) {
		AjaxUtil.setDropDown(dropDown, hiddenField, data, "value", "label");
	};
	supportService.getAllWaitInfoReason({preHook:preHookFn, callback:callbackFn,async:false});
};
AjaxUtil.setAllPendingReason = function(dropDown, hiddenField) {
	var preHookFn = function () {
		AjaxUtil.preHookOptions(dropDown, true);
	};
	var callbackFn = function ( data ) {
		AjaxUtil.setDropDown(dropDown, hiddenField, data, "value", "label");
	};
	supportService.getAllPendingReason({preHook:preHookFn, callback:callbackFn,async:false});
};
